home *** CD-ROM | disk | FTP | other *** search
- unit ADOBatch1U1;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Grids, DBGrids, ExtCtrls, DBCtrls, Db, ADODB, StdCtrls, ComCtrls;
-
- type
- TForm1 = class(TForm)
- ADOTable1: TADOTable;
- DataSource1: TDataSource;
- PageControl1: TPageControl;
- TabSheet1: TTabSheet;
- TabSheet2: TTabSheet;
- Panel1: TPanel;
- DBNavigator1: TDBNavigator;
- Button1: TButton;
- Button2: TButton;
- rgrFilterGroup: TRadioGroup;
- CheckBox1: TCheckBox;
- DBGrid1: TDBGrid;
- rgrSupports: TRadioGroup;
- lblSupports: TLabel;
- Button3: TButton;
- rgrCancelBatch: TRadioGroup;
- StatusBar1: TStatusBar;
- gbxChangeLog: TGroupBox;
- mmoChangeLog: TMemo;
- Button4: TButton;
- ADOConnection1: TADOConnection;
- Button5: TButton;
- cbxUpdateResync: TCheckBox;
- btnDisconnect: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure rgrFilterGroupClick(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- procedure rgrSupportsClick(Sender: TObject);
- procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
- DataCol: Integer; Column: TColumn; State: TGridDrawState);
- procedure Button3Click(Sender: TObject);
- procedure ADOTable1RecordChangeComplete(DataSet: TCustomADODataSet;
- const Reason: TEventReason; const RecordCount: Integer;
- const Error: Error; var EventStatus: TEventStatus);
- procedure Button4Click(Sender: TObject);
- procedure Button5Click(Sender: TObject);
- procedure ADOTable1AfterScroll(DataSet: TDataSet);
- procedure FormCreate(Sender: TObject);
- procedure btnDisconnectClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure ShowData;
- procedure UpdateChangeLog(strChange: string);
- procedure ShowErrors;
- function OriginalValueByName(ADODataSet: TCustomADODataSet;
- strFieldName: string): string;
- function UnderlyingValueByName(ADODataSet: TCustomADODataSet;
- strFieldName: string): string;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- ADOTable1.UpdateBatch;
- end;
-
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- case rgrCancelBatch.ItemIndex of
- 0: ADOTable1.CancelBatch(arCurrent);
- 1: ADOTable1.CancelBatch(arFiltered);
- 2: ADOTable1.CancelBatch(arAll);
- 3: ADOTable1.CancelBatch(arAllChapters);
- end;
- end;
-
- procedure TForm1.rgrFilterGroupClick(Sender: TObject);
- begin
- case rgrFilterGroup.ItemIndex of
- 0: ADOTable1.FilterGroup:=fgNone;
- 1: ADOTable1.FilterGroup:=fgPendingRecords;
- 2: ADOTable1.FilterGroup:=fgAffectedRecords;
- 3: ADOTable1.FilterGroup:=fgFetchedRecords;
- 4: ADOTable1.FilterGroup:=fgPredicate;
- 5: ADOTable1.FilterGroup:=fgConflictingRecords;
- end;
-
- CheckBox1.Checked:=ADOTable1.Filtered;
- end;
-
- procedure TForm1.CheckBox1Click(Sender: TObject);
- begin
- ADOTable1.Filtered:=CheckBox1.Checked;
- end;
-
- procedure TForm1.rgrSupportsClick(Sender: TObject);
- var
- blnSupports: boolean;
- begin
- blnSupports:=False;
- case rgrSupports.ItemIndex of
- 0 : blnSupports:=ADOTable1.Supports([coHoldRecords ]);
- 1 : blnSupports:=ADOTable1.Supports([coMovePrevious ]);
- 2 : blnSupports:=ADOTable1.Supports([coAddNew ]);
- 3 : blnSupports:=ADOTable1.Supports([coDelete ]);
- 4 : blnSupports:=ADOTable1.Supports([coUpdate ]);
- 5 : blnSupports:=ADOTable1.Supports([coBookmark ]);
- 6 : blnSupports:=ADOTable1.Supports([coApproxPosition ]);
- 7 : blnSupports:=ADOTable1.Supports([coUpdateBatch ]);
- 8 : blnSupports:=ADOTable1.Supports([coResync ]);
- 9 : blnSupports:=ADOTable1.Supports([coNotify ]);
- 10: blnSupports:=ADOTable1.Supports([coFind ]);
- 11: blnSupports:=ADOTable1.Supports([coSeek ]);
- 12: blnSupports:=ADOTable1.Supports([coIndex ]);
- end;
- if blnSupports then
- lblSupports.Caption:='Supported'
- else
- lblSupports.Caption:='Not Supported';
- end;
-
- procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
- DataCol: Integer; Column: TColumn; State: TGridDrawState);
- begin
- case ADOTable1.UpdateStatus of
- usModified: DBGrid1.Canvas.Brush.Color:=clBlue;
- usDeleted : DBGrid1.Canvas.Brush.Color:=clRed;
- usInserted: DBGrid1.Canvas.Brush.Color:=clGreen;
- end;
-
- DBGrid1.DefaultDrawDataCell(Rect, Column.Field, State);
- end;
-
- procedure TForm1.Button3Click(Sender: TObject);
- begin
- ADOTable1.CancelUpdates
- end;
-
- procedure TForm1.ADOTable1RecordChangeComplete(DataSet: TCustomADODataSet;
- const Reason: TEventReason; const RecordCount: Integer;
- const Error: Error; var EventStatus: TEventStatus);
- begin
- case Reason of
- erAddNew : UpdateChangeLog('A new row was added.');
- erDelete : UpdateChangeLog('An existing row was deleted.');
- erUpdate : UpdateChangeLog('An existing row was modified with new values.');
- erUndoUpdate : UpdateChangeLog('An update operation was canceled.');
- erUndoAddNew : UpdateChangeLog('A row insert operation was canceled.');
- erUndoDelete : UpdateChangeLog('A row delete operation was canceled.');
- erRequery : UpdateChangeLog('The recordset was refreshed with the Requery method.');
- erResynch : UpdateChangeLog('The recordset was resynchronized with the Resynch method.');
- erClose : UpdateChangeLog('The recordset was closed.');
- erMove : UpdateChangeLog('The recordsets row pointer moved.');
- erFirstChange : UpdateChangeLog('Record changed for first time.');
- erMoveFirst : UpdateChangeLog('The recordsets row pointer moved to the first row.');
- erMoveNext : UpdateChangeLog('The recordsets row pointer moved to the next row.');
- erMovePrevious: UpdateChangeLog('The recordsets row pointer moved to the previous row.');
- erMoveLast : UpdateChangeLog('The recordsets row pointer moved to the last row.');
- end;
-
- case EventStatus of
- esOK : UpdateChangeLog('Operation executed without problem.');
- esErrorsOccured : UpdateChangeLog('An error occurred during execution of the operation.');
- esCantDeny : UpdateChangeLog('A pending connection operation cannot be canceled. (Connection events only.).');
- esCancel : UpdateChangeLog('A pending connection has been canceled before it became active. (Connection events only.)');
- esUnwantedEvent : UpdateChangeLog('Set by the ADO method, prevents subsequent notification of the event.');
- end;
- end;
-
- procedure TForm1.ShowData;
- begin
- ShowMessage('An error occurred'+#13+
- 'Value: '+ADOTable1.FieldByName('ContactName').AsString+#13+
- 'Original: '+OriginalValueByName(ADOTable1, 'ContactName')+#13+
- 'Underlying: '+UnderlyingValueByName(ADOTable1, 'ContactName'));
- end;
-
- procedure TForm1.UpdateChangeLog(strChange: string);
- begin
- // StatusBar1.SimpleText:=strChange;
- mmoChangeLog.Lines.Add(strChange);
- end;
-
- procedure TForm1.Button4Click(Sender: TObject);
- begin
- mmoChangeLog.Clear;
- end;
-
- procedure TForm1.Button5Click(Sender: TObject);
- begin
- ADOConnection1.BeginTrans;
- try
- ADOTable1.UpdateBatch;
- ADOConnection1.CommitTrans;
- except
- on E: Exception do
- begin
- ADOConnection1.RollbackTrans;
- // ShowErrors;
- ShowMessage('Errors occurred ('+E.ClassName+')'+#13+'Complete batch rolled back');
- end;
- end;
- end;
-
- procedure TForm1.ShowErrors;
- var
- Err: Error;
- str: string;
- intError: integer;
- begin
- str:='';
- for intError:=0 to ADOConnection1.Errors.Count - 1 do
- begin
- Err:=ADOConnection1.Errors.Item[intError];
- str:=str+Err.Description+#13;
- end;
- if str <> '' then
- ShowMessage(str);
- end;
-
- function TForm1.OriginalValueByName(ADODataSet: TCustomADODataSet; strFieldName: string): string;
- var
- Field: TField;
- begin
- Field:=ADODataSet.FieldByName(strFieldName);
- Result:=ADODataSet.Recordset.Fields[Field.FieldNo - 1].OriginalValue;
- end;
-
- function TForm1.UnderlyingValueByName(ADODataSet: TCustomADODataSet; strFieldName: string): string;
- var
- Field: TField;
- begin
- Field:=ADODataSet.FieldByName(strFieldName);
- Result:=ADODataSet.Recordset.Fields[Field.FieldNo - 1].UnderlyingValue;
- end;
-
- procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet);
- begin
- ADOTable1.UpdateCursorPos;
-
- StatusBar1.Panels[0].Text:=
- 'Value: '+ADOTable1.FieldByName('ContactName').AsString+
- ' NewValue: '+ADOTable1.FieldByName('ContactName').NewValue;
-
- StatusBar1.Panels[1].Text:=
- 'Original: '+OriginalValueByName(ADOTable1, 'ContactName')+
- ' OldValue: '+ADOTable1.FieldByName('ContactName').OldValue;
-
- // Jet 4.0 OLE DB Provider does not return the correct value for
- // UnderlyingValue/CurValue
- StatusBar1.Panels[2].Text:=
- 'Underlying: '+UnderlyingValueByName(ADOTable1, 'ContactName')+
- ' CurValue: '+ADOTable1.FieldByName('ContactName').CurValue;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- cbxUpdateResync.Checked:=ADOTable1.Properties['Update Resync'].Value=1;
- end;
-
- procedure TForm1.btnDisconnectClick(Sender: TObject);
- begin
- if ADOTable1.Connection=nil then
- begin
- ADOConnection1.Connected:=True;
- ADOTable1.Connection:=ADOConnection1;
- btnDisconnect.Caption:='Disconnect';
- end
- else
- begin
- ADOTable1.Connection:=nil;
- ADOConnection1.Connected:=False;
- btnDisconnect.Caption:='Connect';
- end
- end;
-
- end.
-